<template>
  <div id="userCenter">
    <!-- 导航 -->
    <van-nav-bar
      :title="$t('mine.personalInfo')"
      :right-text="$t('mine.confirm')"
      left-arrow
      @click-left="$router.back()"
      @click-right="saveUser"
    />
    <!-- 输入框 -->
    <div class="icon">
      <span class="title">{{ $t('mine.head') }}</span>
      <img
        src="https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2841648446,236398816&fm=26&gp=0.jpg"
        alt
      />
    </div>
    <van-cell-group v-for="(item, index) in setList" :key="index">
      <van-field v-model="item.username" :label="$t('mine.user_name')" />
      <van-field v-model="item.mobile" :label="$t('mine.phoneNumber')" />
      <van-field v-model="item.email" :label="$t('mine.email')" />
      <div class="sex">
        <span style="float:left">{{ $t('mine.userSex') }}</span>
        <van-radio-group v-model="item.sex" style="float:left;margin-left:64px">
          <van-radio :name="0" checked-color="#07c160" style="float:left">{{ $t('mine.woman') }}</van-radio>
          <van-radio :name="1" checked-color="#07c160" style="float:left;margin-left:30px">{{ $t('mine.man') }}</van-radio>
        </van-radio-group>
      </div>
    </van-cell-group>
    <van-button type="danger" @click="logout" size="large">{{ $t('mine.logout') }}</van-button>
  </div>
</template>

<script>
import { Toast, Dialog } from 'vant'
export default {
  data () {
    return {
      // 个人设置数组
      setList: []
    }
  },
  methods: {
    // 修改信息保存
    saveUser () {
      // 调用修改接口
      this.$http
        .put(`/setting/${this.setList[0].id}`, this.setList[0])
        .then(res => {
          if (res.data.ok !== 1) {
            return this.$toast.fail(res.data.error)
          }
          // 修改成功
          this.$toast.success('修改信息成功')
          this.$router.push('/mine')
        })
    },
    // 退出
    logout () {
      Dialog.confirm({
        // title: '温馨提示：',
        message: this.$t('mine.loginInfo')
      })
        .then(() => {
          // 清除token
          localStorage.clear()
          // 去登录页
          this.$router.push('/login')
          Toast({
            message: this.$t('mine.infoSuccess'),
            duration: 800
          })
        })
        .catch(() => {
          // on cancel
        })
    }
  },
  created () {
    // 获取浏览器里存的id
    let id = localStorage.getItem('userId')
    // 调用设置接口
    this.$http.get(`/setting/${id}`).then(res => {
      this.setList = res.data.data
    })
  }
}
</script>

<style lang="less">
#userCenter {
  .icon {
    display: flex;
    height: 3rem;
    width: 100%;
    // margin-top: 3rem;
    padding: 0 16px;
    background-color: #ffffff;
    align-items: center;
    .title {
      height: 3rem;
      line-height: 3rem;
      color: #323233;
      font-size: 14px;
    }
    img {
      position: absolute;
      right: 1.6rem;
      height: 2.5rem;
      width: 2.5rem;
    }
  }
  .van-field__control {
    color: #a2a2a2;
    font-size: 13px;
  }

  .van-field__label {
    font-size: 13px;
  }

  .van-button {
    margin-top: 20px;
  }

  .sex {
    font-size: 12px;
    padding: 10px;
    padding-left: 18px;
    line-height: 20px;
  }
  .sex::after {
    content: "";
    display: block;
    clear: both;
  }
}
</style>
